/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nl.hro.mobiqueen.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author jv
 */
public class GoogleMapItems extends ItemizedOverlay{

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private Road mRoad;
    private GoogleRoadParameters mRoadParameters;
    
    public GoogleMapItems(Drawable defaultMarker)
    {
        super(boundCenterBottom(defaultMarker));
    }

    public GoogleMapItems(Drawable defaultMarker, Context context, GoogleRoadParameters parameters)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        mRoadParameters = parameters;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    private OverlayItem clickedItem;

    @Override
    protected boolean onTap(int index) 
    {  
        clickedItem = mOverlays.get(index);
        //Toast toast = Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT);
        //toast.show();

        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(clickedItem.getTitle());
        dialog.setMessage(clickedItem.getSnippet());
        dialog.setPositiveButton("Navigate Here", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id) {
                initializeRoad();
            }
        });

        dialog.show();
        return true;
    }

    private void initializeRoad(){
        new Thread() {
            @Override
            public void run() {

                Double toLat = clickedItem.getPoint().getLatitudeE6() / 1E6;
                Double toLon = clickedItem.getPoint().getLongitudeE6() / 1E6;

                try{
                    String url = RoadProvider.getUrl(
                                            mRoadParameters.getStartLocationLat(),
                                            mRoadParameters.getStartLocationLon(),
                                            toLat,toLon);

                    InputStream is = getConnection(url);
                    mRoad = RoadProvider.getRoute(is);
                    mHandler.sendEmptyMessage(0);
                }catch(Exception e){

                }
            }
        }.start();
    }

    Handler mHandler = new Handler() {
    @Override
            public void handleMessage(android.os.Message msg) {

                    //TextView textView = (TextView) findViewById(R.id.description);
                    //textView.setText(mRoad.mName + " " + mRoad.mDescription);
                    RoadOverlay mapOverlay = new RoadOverlay(mRoad, mRoadParameters.getMapview());
                    List<Overlay> listOfOverlays = mRoadParameters.getMapview().getOverlays();
                    //listOfOverlays.clear();
                    listOfOverlays.add(mapOverlay);
                    mRoadParameters.getMapview().invalidate();
            };
    };

    private InputStream getConnection(String url) {
            InputStream is = null;
            try {
                    URLConnection conn = new URL(url).openConnection();
                    is = conn.getInputStream();
            } catch (MalformedURLException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
            return is;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }
}
